const db=uniCloud.database()
import {adjustArrayOrder} from '@/utils/common.js'

export default {
    namespaced: true,
    state: {
		// 表头数据
		tableHeard: [],
		// 需要展示的表头
		showTableHeard:[],
		// 表格数据
        thread_tree_data: [],
		// 总条数
		total: 1,
		// 当前页码
		current: 1,
		// 每页多少条数据
		pageSize: 5,
		// 当前选中的客户数据
		clientInfo:{},
		// popup状态,无论true或false，一旦改变
		showPopup: false,
		// 当前跟进记录的数据
		followRecord:[],
		
		
    },
    mutations: {
		// 修改表格、表头数据
		setThreadData(state, data) {  
			state.thread_tree_data = data;
			state.thread_tree_data = [...data, ...data, ...data];
			const excludeKeys = ['_id', 'allocationStatus', 'clientStatus', 'uploadCount', 'publish_ip', 'user_id']; 
			state.tableHeard = adjustArrayOrder(['index', ...Object.keys(data[0]).filter(item => !excludeKeys.includes(item)), 'operation']);
		}, 
		// 修改分页器总条数
		setTotal(state, data) {  
			state.total = data; 
		}, 
		// 修改表格选中行的数据
		setClientInfo(state, data) {
			state.showPopup = !state.showPopup
			state.clientInfo = data; 
		},
		// 修改当前选中行的跟进记录
		setFollowRecord(state, data) {
			console.log(data);
			
			state.followRecord = data.reverse()
			console.log(state.followRecord);
		},
		// 跟进输入框数据
		setFollowInfo(state) {
			state.followInfo = ''
			console.log(state.followInfo);
		},
		
    },
    actions: {
		// 获取客户数据
		getThreadData({commit, state}) {
			db.collection('zf_customerInfo')
			  .skip((state.current-1) * state.pageSize)
			  .limit(state.pageSize) 
			  .get()
			  .then(res => {
			  	// console.log(res)
			  	if (res.errCode==0 && res.result.data.length > 0) {  
			  		const data = res.result.data;  
			  		commit('setThreadData', data);  
			  	}  
			  }).catch(err => {  
			  	console.log(err)
			  })
		},
		
		// 获取数据总数
		getDataCount({ commit }){
			db.collection('zf_customerInfo').count().then(res=>{
				if(res.errCode === 0){
					commit('setTotal', res.result.total)
				}
			}).catch(err=>{
				console.log(err)
			})
		},
		
		// 提交跟进记录
		async addFollowRecord({commit, state, dispatch}, obj = {}){
			// 1、获取当前客户对应的跟进记录
			let res = await db.collection('zf_client_followRecord').where({ client_id: state.clientInfo._id }).get()
			let code = null;
			if (res.result.data.length > 0) {  
				// 2、如果存在，则更新 data 数组  
				
				let r =  await db.collection('zf_client_followRecord')
					.doc( res.result.data[0]._id)
					.update({ data: [...res.result.data[0].data, obj ] })
				code = r.errCode
				
			} else {  
				// 3、如果不存在，则插入新记录
				const newRecord = {  
					client_id: state.clientInfo._id,
					data: [obj]
				};  
				let r = await db.collection('zf_client_followRecord').add(newRecord);
				code = r.errCode
			}
			
			if(code === 0){
				uni.showToast({ title: "提交成功", icon: "none" })
				commit('setFollowInfo')
				dispatch('getFollowRecord')
				return true
			}else{
				uni.showToast({ title: "提交失败", icon: "error" })
				return false
			}
		},
		
		// 获取跟进记录
		getFollowRecord({ commit, state}){
			console.log(state);
			db.collection('zf_client_followRecord').where({
				client_id:state.clientInfo._id,
			}).get().then(res=>{
				console.log(res);
				if(res.errCode === 0){
					commit('setFollowRecord', res.result.data[0].data)
				}
			}).catch(err=>{
				console.log(err)
			})
		},
    }
}


